<template>
  <div class="qualiy_list">
    <div class="monitor-tree">
      <h3 class="tree-title">选择洞幅</h3>
      <el-tree
          :data="tunnelInfoChild"
          @check-change="treeCheckClick"
          :default-expanded-keys="keysListNodeData"
          :check-on-click-node="true"
          node-key="pid"
          ref="tree"
          @node-click="nodetest"
      ></el-tree>
    </div>

    <div class="monitor-list">
      <PredictionInfoHeaderVue  @searchReportNoPrediction ="changeData"   />
      <el-table :data="tunnelInfo" :header-cell-style="{ background: '#f8f8f9',textAlign: 'center' }" :cell-style="{'text-align':'center'}">
        <el-table-column width="199" prop="remarks" label="报告编号"></el-table-column>
        <el-table-column width="100" prop="startMileage" label="开始里程"></el-table-column>
        <el-table-column width="100" prop="endMileage" label="结束里程"></el-table-column>
        <el-table-column width="60" prop="surroundGrade" label="围岩等级"></el-table-column>
        <el-table-column width="60" prop="predictionGrade" label="预报级别"></el-table-column>
        <el-table-column width="100" prop="predictionType" label="预报手段"></el-table-column>
        <el-table-column show-overflow-tooltip prop="predictionResult" label="预报结果"></el-table-column>
        <el-table-column
            width="100"
            :formatter="formatDate"
            show-overflow-tooltip
            prop="predictionTime"
            label="预报时间"
        ></el-table-column>
        <el-table-column width="250"  label="操作">
          <template slot-scope="scope">
            <el-button
                size="mini"
                type="primary"
                icon="el-icon-search"
                @click="preview(scope.$index, scope.row)"
            >查看
            </el-button>
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->

      <el-pagination
          @current-change="handleCurrentChange"
          layout="total, sizes, prev, pager, next, jumper"
          :total="currentPage.total"
      ></el-pagination>

      <!-- 分页 -->
    </div>

    <PredictionEditor :flag="flag"/>
    <el-dialog :visible="isVisible" :before-close="handleClose" fullscreen>
      <pdfTemp :pdfsrc="pdfsrc" />
    </el-dialog>
  </div>
</template>

<script>
import api from '../../../api'
import base from '../../../api/base'
import qs from 'qs'
import PredictionInfoHeaderVue from './PredictionInfoHeader.vue'
import PredictionEditor from './PredictionEditor.vue'

export default {
  data() {
    return {
      keysListNodeData:[],
      wId: '', // 洞幅id
      tunnelInfoChild: [],
      currentPage: {
        pageNo: 1,
        total: 0
      },
      tunnelInfo: [],
      src: null,
      flag: false,
      isVisible: false,
      pdfsrc: "",
    }
  },
  mounted() {
    this.getchildren()
    this.$bus.$on('onRefreshPrediction', (flag) => {
      this.http(1, flag)
    })

  },
  components: {
    PredictionInfoHeaderVue,
    PredictionEditor
  },
  methods: {
    //获取数结构
    getchildren() {
      api
          .tunnelWidthFindByClid()
          .then((res) => {
            if (res.data.code == 2001) {
              this.tunnelInfoChild = res.data.data
              const arrDat = res.data.data[0].pid
            this.keysListNodeData.push(arrDat)
            }
          })
          .catch((error) => {
            console.log(error)
          })
    },
    http(page, wId ,tId) {
      api
          .advanceInfopageList({page, wId ,tId})
          .then((res) => {
            console.log(res)
            if (res.data.code == 2001) {
              this.tunnelInfo = res.data.data.records
              this.currentPage.total = parseInt(res.data.data.total)
            }
          })
          .catch((error) => {
            console.log(error)
          })
    },
    handleEdit(index, row) {
      this.$bus.$emit('onShowPredictionEditor', {rowdata: row, wId: this.wId})
    },
    handleDelete(index, row) {
      let data = {
        id: row.id
      }
      this.$confirm('此操作将永久删除超前预报结果, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        customClass: 'del-model',
        type: 'warning'
      })
          .then(() => {
            api
                .advanceInfodeleteById(qs.stringify(data))
                .then((res) => {
                  if (res.data.code == 2001) {
                    this.$message({
                      type: 'success',
                      message: '删除成功!'
                    })
                    // 视图重绘
                    this.http(1, this.wId)
                  } else {
                    this.$message({
                      type: 'fail',
                      message: '删除失败!'
                    })
                  }
                })
                .catch((err) => {
                  console.log(err)
                })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
          })
    },
    preview(index, row) {
      if(row.resources == ''){
         this.$message({
                  type: 'fail',
                  message: '附件为空!'
                })
      }else{
       this.pdfsrc = base.filebaseUrl + row.resources
       this.$router.push('/pdf/' + encodeURIComponent(this.pdfsrc))
      }
    },
    handleCurrentChange(row) {
      this.http(row, this.wId)
    },
    treeCheckClick(data, checked, node) {
      if (data.id != undefined) {
        this.wId = data.id
        this.http(1, this.wId)
        this.$bus.$emit('onAddPredictionWid', this.wId)
      }
    },
    formatDate(row, column) {
      // 获取单元格数据
      let data = row[column.property]
      if (data == null) {
        return null
      }
      let dt = new Date(data)
      return dt.getFullYear() + '-' + (dt.getMonth() + 1) + '-' + dt.getDate()
    },
    changeData(reportNo){
      api.advanceInfofindByAndRemarks({remarks:reportNo}).then(res=>{
        if(res.data.code == 2001){
          this.tunnelInfo = res.data.data.records
           this.currentPage.total = parseInt(res.data.data.total)
        }
      }).catch(err=>{
          console.log(err)
      })
    },
    nodetest(data, checked, node){
      if (data.tid != undefined) {
        this.http(1,null,data.tid)
      }
    },// 关闭
    handleClose() {
      this.isVisible = false;
    },
  }
}
</script>

<style scoped>
.tree-title {
  text-align: left;
  font-size: 16px;
  color: #606266;
  padding: 10px;
  padding-bottom: 20px;
}

.monitor-tree {
  width: 300px;
  border-left: 1px solid #f1f1f1;
  border-right: 1px solid #f1f1f1;
  background-color: #fff;
}

.monitor-list {
  flex: 1;
  margin-left: 10px;
  width: 100%;
}
</style>